package com.arqui.admmon_module.server.services;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.xml.rpc.ServiceException;

import com.arqui.admmon_module.server.webservices.eventsmonitor.InvalidTransaction;
import com.arqui.admmon_module.server.webservices.eventsmonitor.NonexistentEntityException;
import com.arqui.admmon_module.server.webservices.eventsmonitor.UnknownHostException;
import com.arqui.admmon_module.server.webservices.eventsmonitor.WebServices_PortType;
import com.arqui.admmon_module.server.webservices.eventsmonitor.WebServices_ServiceLocator;
import com.arqui.admmon_module.shared.InvalidsEntity;


public class InvalidTransactionsService {
	
	//To find invalid transactions by syntax in historical mode
	public List<InvalidsEntity> getNotSytaxTransactionHistoric(
			Date initDate, Date finalDate) throws UnknownHostException,
			NonexistentEntityException, RemoteException, ServiceException,
			Exception {
		Calendar date1 = Calendar.getInstance();
		Calendar date2 = Calendar.getInstance();
		date1.setTime(initDate);
		date2.setTime(finalDate);
		String initialDate = dateToString(date1);
		String endDate = dateToString(date2);

		return mapInvalids(getInvalidBySyntax(initialDate, endDate));

	}

	//To find invalid transactions by syntax in real time mode
	public List<InvalidsEntity> getNotSyntaxTransactionRealTime()
			throws UnknownHostException, NonexistentEntityException,
			RemoteException, ServiceException, Exception {
		Calendar finalDate = Calendar.getInstance();
		Calendar initDate = finalDate;
		initDate.set(Calendar.SECOND, finalDate.get(Calendar.SECOND) - 5);
		
		String initialDate = dateToString(initDate);
		String endDate = dateToString(finalDate);

		return mapInvalids(getInvalidBySyntax(initialDate, endDate));

	}

	// To find invalid transactions by identifiaction in historical mode
	public List<InvalidsEntity> getNotIdentificationTransactionHistoric(
			Date initDate, Date finalDate) throws UnknownHostException,
			NonexistentEntityException, RemoteException, ServiceException,
			Exception {
		Calendar date1 = Calendar.getInstance();
		Calendar date2 = Calendar.getInstance();
		date1.setTime(initDate);
		date2.setTime(finalDate);
		String initialDate = dateToString(date1);
		String endDate = dateToString(date2);

		return mapInvalids(getInvalidByIdentification(initialDate, endDate));

	}
	
	// To find invalid transactions by identification in real time mode
	public List<InvalidsEntity> getNotIdentificationTransactionRealTime()
			throws UnknownHostException, NonexistentEntityException,
			RemoteException, ServiceException, Exception {
		Calendar finalDate = Calendar.getInstance();
		Calendar initDate = finalDate;
		initDate.set(Calendar.SECOND, finalDate.get(Calendar.SECOND) - 5);

		String initialDate = dateToString(initDate);
		String endDate = dateToString(finalDate);

		return mapInvalids(getInvalidByIdentification(initialDate, endDate));

	}
		
	// To find invalid transactions by card in historical mode
	public List<InvalidsEntity> getNotCardTransactionHistoric(
			Date initDate, Date finalDate) throws UnknownHostException,
			NonexistentEntityException, RemoteException, ServiceException,
			Exception {
		Calendar date1 = Calendar.getInstance();
		Calendar date2 = Calendar.getInstance();
		date1.setTime(initDate);
		date2.setTime(finalDate);
		String initialDate = dateToString(date1);
		String endDate = dateToString(date2);

		return mapInvalids(getInvalidByCard(initialDate, endDate));

	}
	
	// To find invalid transactions by card in real time mode
	public List<InvalidsEntity> getNotCardTransactionRealTime()
			throws UnknownHostException, NonexistentEntityException,
			RemoteException, ServiceException, Exception {
		Calendar finalDate = Calendar.getInstance();
		Calendar initDate = finalDate;
		initDate.set(Calendar.SECOND, finalDate.get(Calendar.SECOND) - 5);

		String initialDate = dateToString(initDate);
		String endDate = dateToString(finalDate);

		return mapInvalids(getInvalidByCard(initialDate, endDate));

	}
		
	private InvalidTransaction[] getInvalidByIdentification(
			String initDate, String endDate) throws ServiceException,
			Exception, UnknownHostException, NonexistentEntityException,
			RemoteException {
		WebServices_ServiceLocator service = new WebServices_ServiceLocator();
		WebServices_PortType port = service.getWebServicesPort();
		return port.getInvalidByIdentification(initDate, endDate);
	}

	private InvalidTransaction[] getInvalidByCard(String initDate,
			String endDate) throws ServiceException, Exception,
			UnknownHostException, NonexistentEntityException, RemoteException {
		WebServices_ServiceLocator service = new WebServices_ServiceLocator();
		WebServices_PortType port = service.getWebServicesPort();
		return port.getInvalidByCard(initDate, endDate);
	}

	private InvalidTransaction[] getInvalidBySyntax(String initDate,
			String endDate) throws ServiceException, Exception,
			UnknownHostException, NonexistentEntityException, RemoteException {
		WebServices_ServiceLocator service = new WebServices_ServiceLocator();
		WebServices_PortType port = service.getWebServicesPort();
		return port.getNotSyntaxTransaction(initDate, endDate);
	}

	private String dateToString(Calendar date) {
		String stringDate = "" + date.get(Calendar.YEAR) + "-"
				+ date.get(Calendar.MONTH) + "-"
				+ date.get(Calendar.DAY_OF_MONTH) + "-"
				+ date.get(Calendar.HOUR_OF_DAY) + "-"
				+ date.get(Calendar.MINUTE) + "-" + date.get(Calendar.SECOND);
		return stringDate;
	}
	
	private List<InvalidsEntity> mapInvalids(InvalidTransaction[] list){
		List<InvalidsEntity> finalList = new ArrayList<InvalidsEntity>();
		for(InvalidTransaction e:list){
			InvalidsEntity temp = new InvalidsEntity();
			temp.setDate(e.getDate());
			temp.setBranch(e.getEstablishment());
			temp.setFranchise(e.getFranshise());
			temp.setId(e.getIdFile());
			temp.setLine(e.getNumberLine());
			temp.setStore(e.getBrand());
			finalList.add(temp);
		}
		return finalList;
	}

}
